<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>车享乐-商品列表</title>
	<link rel="stylesheet" type="text/css" href="/static/css/reset.css">
	<link rel="stylesheet" type="text/css" href="/static/css/main.css">
	<script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript" src="/static/js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="/static/js/slide.js"></script>

	<script type="text/javascript" src="../../static/layui/layui.js"></script>
	<link href="../../static/layui/css/layui.css" rel="stylesheet" rel="stylesheet">
	<script src="../../static/mylayer.js" type="text/javascript" charset="utf-8"></script>
	<style>
		.avatar-img {
			width: 20px; /* 设置头像图片的宽度 */
			height: 20px; /* 设置头像图片的高度，可以根据需要调整 */
			border-radius: 50%; /* 使图片呈现圆形 */
			margin-right: 10px; /* 设置头像图片与用户名之间的间距，可以根据需要调整 */
		}
	</style>
</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到鑫鑫车享乐租车系统!  祝您畅享旅途，鑫鑫相伴！</div>
			<div class="fr">
				<div class="user_link fl">
					<a href="javascript:;">
						欢迎您：<img class="avatar-img" src="/pic/${user.avatar}"/>
						${user.username}
					</a>
					<span>|</span>
					<a href="/page/user/info">用户中心</a>
					<span>|</span>
					<a href="/page/user/order">我的订单</a>
					<span>|</span>
					<a href="javascript:logout()">注销</a>
				</div>
			</div>
		</div>
	</div>

	<div class="search_bar clearfix">
		<a href="/" class="logo fl"><img src="/static/pic/xx-logo.png" width="180%"></a>
		<div class="search_con fl">
			<input type="text" id="searchInput" class="input_text fl" placeholder="搜索商品">
			<input type="button" id="searchButton" class="input_btn fr" value="搜索">
		</div>
	</div>

	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部车辆品牌</h1>
				<span></span>			
				<ul class="subnav" id="LAY_BrandList">
<%--					<li><a href="#" class="fruit">新鲜水果</a></li>--%>
<%--					<li><a href="#" class="seafood">海鲜水产</a></li>--%>
<%--					<li><a href="#" class="meet">猪牛羊肉</a></li>--%>
<%--					<li><a href="#" class="egg">禽类蛋品</a></li>--%>
<%--					<li><a href="#" class="vegetables">新鲜蔬菜</a></li>--%>
<%--					<li><a href="#" class="ice">速冻食品</a></li>--%>
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="/">首页</a></li>
				<li class="interval">|</li>
				<li><a href="/page/car/list">全部车辆</a></li>
			</ul>
		</div>
	</div>

	<div class="breadcrumb" id="LAY_brandName">

	</div>

	<div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>新品推荐</h3>
				<ul id="LAY_CarListNew">

				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<div class="sort_bar">
				<a href="#" class="active">默认</a>
				<a href="#">价格</a>
				<a href="#">人气</a>
			</div>

			<ul class="goods_type_list clearfix" id="LAY_CarList">


			</ul>

			<div id="demo-laypage-all"></div>
		</div>
	</div>
	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>

	<script>
		$.post(
				'/brand?method=selectByBrand',
				function (result) {
					if (result.code == 0) {
						$('#LAY_BrandList').empty();
						$(result.data).each(function () {
							$('#LAY_BrandList').append('<li><a href="list?brandId=' + this.brandId + '">'+this.brandName+'</a></li>');
						})
					}
				},
				'json'
		);
		var queryString = window.location.search;
		var urlParams = new URLSearchParams(queryString);
		var brandId = urlParams.get("brandId");
		$.post(
				'/brand?method=selectByBrandName',
				{'brandId':brandId},
				function (result) {
					if (result.code == 0) {
						$('#LAY_brandName').empty();
						var html = '';
						$(result.data).each(function () {
							html += '<a href="#">全部分类</a>'
							html += '<span>></span>'
							html += '<a href="#">'+this.brandName+'</a>'
						})
						$('#LAY_brandName').append(html);
					}
				},
				'json'
		);
		$.post(
				'/car?method=selectByBrandCar',
				{ 'brandId': brandId },
				function (result) {
					if (result.code == 0) {
						var html = '';
						$('#LAY_CarListNew').empty();
						$(result.data.slice(-2)).each(function () {

							html += '<li>'
							html += '  <a href="/page/detail?carId=' + this.id + '&brandId=' + this.brandId + '"><img src="/pic/' + this.image + '"></a>'
							html += '	 <h4><a href="/page/detail?carId=' + this.id + '&brandId=' + this.brandId + '">' + this.model + '</a></h4>'
							html += '	 <div className="prize">¥ ' + this.rentalPrice + '</div>'
							html += '</li>'
						});
						$('#LAY_CarListNew').append(html);
					}
				},
				'json'
		);
		var brandCarQuery = {
			page : 1,
			limit : 10,
			model : null,
			brandId : brandId
		};
		// 定义全局变量，用于保存分页组件的实例
		var laypageInstance = null;
		// 绑定搜索按钮点击事件
		$('#searchButton').click(function() {
			updateCarQuery(); // 更新 carQuery 对象
			brandCarQuery.page=1;
			sendQuery(); // 发送查询请求
		});

		// 页面加载完成后自动执行一次查询
		$(document).ready(function() {
			sendQuery();
		});

		function updateCarQuery() {
			var searchText = $('#searchInput').val(); // 获取搜索框中的值
			brandCarQuery.model = searchText || null; // 更新 carQuery 中的 model 字段
		}
		function sendQuery() {
			$.post(
					'/car?method=selectByCarPage',
					brandCarQuery, // 将 carQuery 对象作为数据发送
					function (result) {
						if (result.code == 0) {
							renderCarList(result);
							page(result.count);
							console.log(result.data);
							console.log(result.count);
							// 更新分页组件的总页数
						}
					},
					'json'
			);
		}


		function renderCarList(result) {
			console.log(brandCarQuery)
			if (result.code == 0) {
				var carListHtml = '';
				$('#LAY_CarList').empty();
				$(result.data).each(function () {
					carListHtml += '<li>'
					carListHtml += '	<a href="/page/detail?carId=' + this.id + '&brandId=' + this.brandId + '"><img src="/pic/' + this.image + '"></a>'
					carListHtml += '	<h4><a href="/page/detail?carId=' + this.id + '&brandId=' + this.brandId + '">' + this.model + '</a></h4>'
					carListHtml += '	<div class="operate">'
					carListHtml += '		<span class="prize">¥ ' + this.rentalPrice + '</span>'
					carListHtml += '		<span class="unit">' + this.rentalPrice + '/天</span>'
					carListHtml += '	</div>'
					carListHtml += '</li>'
				});
				$('#LAY_CarList').append(carListHtml);
			}
		}
		function page(count) {
			layui.use(function(){
				var laypage = layui.laypage;
				// 完整显示
				console.log(count)
				// 初始化分页
				laypageInstance = laypage.render({
					elem: 'demo-laypage-all', // 元素 id
					count: count, // 假设总数据量
					curr: brandCarQuery.page, // 设置当前页码
					limit:brandCarQuery.limit,
					layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'], // 功能布局
					jump: function(obj, first){
						if (!first) { // 首次不执行，避免重复触发
							brandCarQuery.page = obj.curr; // 根据当前页码获取数据
							brandCarQuery.limit = obj.limit;
							sendQuery();
						}
					}
				});
			});
		}
		// $.post(
		// 		'/car?method=selectByBrandCar',
		// 		{ 'brandId': brandId },
		// 		function (carResult) {
		// 			if (carResult.code == 0) {
		// 				var carListHtml = '';
		// 				$('#LAY_CarList').empty();
		// 				$(carResult.data).each(function () {
		// 					carListHtml += '<li>'
		// 					carListHtml += '	<a href="/page/detail?carId=' + this.id + '&brandId=' + this.brandId + '"><img src="/pic/' + this.image + '"></a>'
		// 					carListHtml += '	<h4><a href="/page/detail?carId=' + this.id + '&brandId=' + this.brandId + '">' + this.model + '</a></h4>'
		// 					carListHtml += '<div className="operate">'
		// 					carListHtml += '	<span className="prize">¥ ' + this.rentalPrice + '</span>'
		// 					carListHtml += '	<span className="unit">' + this.rentalPrice + '/天</span>'
		// 					carListHtml += '	</div>'
		// 					carListHtml += '</li>'
		// 				});
		// 				$('#LAY_CarList').append(carListHtml);
		// 			}
		// 		},
		// 		'json'
		// );
		function logout() {
			layer.confirm(
					'您确认要退出么',
					{icon:3},
					function() {
						$.post(
								'/user?method=logout',
								function (result) {
									if (result.code == 0) {
										mylayer.okUrl(result.msg, '/page/login');
									}
								},
								'json'
						);
						// location.href = '/admin?method=logout'
					}
			);
		}
	</script>
</body>
</html>